package com.youthHostel.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageInfo;
import com.youthHostel.pojo.flatplay;
import com.youthHostel.service.FlatplayService;

@Controller
public class FlatplayController {
	@Autowired
	private FlatplayService flatplayService;
	
	@RequestMapping("/listFlatplay")
	public String list(Model model,int index) {
		PageInfo<flatplay> list = flatplayService.findAll(index);
		model.addAttribute("flatplays", list);
		return "flatplay/list";
	}
	@RequestMapping("/deleteFlatplay")
	public String delete(Integer id) {
		flatplayService.deleteByPrimaryKey(id);
		return "redirect:listFlatplay?index=1";
	}
	@RequestMapping("/getFlatplayId")
	public String getLogisticsId(Integer id,Model model) {
		flatplay record = flatplayService.selectByPrimaryKey(id);
		model.addAttribute("flay", record);
		return "flatplay/update";
	}
	@RequestMapping("/updateFlatplay")
	public String update(flatplay record,String bgtime,String endtime) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			record.setFp_start_date(sdf.parse(bgtime + " 00:00:00"));
			record.setFp_end_date(sdf.parse(endtime + " 23:59:59"));
			int row=flatplayService.update(record);
			if(row>0) {
				return "redirect:listFlatplay?index=1";
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return "flatplay/update";
	}
	@RequestMapping("/toaddFlatplay")
	public String toadd() {
		return "flatplay/add";
	}
	@RequestMapping("addFlatplay")
	public String add(flatplay record,String bgtime,String endtime) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			record.setFp_start_date(sdf.parse(bgtime + " 00:00:00"));
			record.setFp_end_date(sdf.parse(endtime + " 23:59:59"));
			flatplayService.insert(record);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "redirect:listFlatplay?index=1";
	}
	
	@RequestMapping("/searchFlatplay")
	public String getLogisticsByName(@RequestParam("name")String name,int index,HttpSession session) {
		List<flatplay> list =flatplayService.findByName(name,index);
		session.removeAttribute("flatplaySearch");
		session.setAttribute("flatplaySearch", list);
		return "flatplay/search";
	}
}
